/**
 *====================================================
 * 文件名称: MaterialTemplateServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.bulkstock.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.entity.MaterialTemplateEntity;
import com.yuexunit.finance.bulkstock.entity.lexicon.MaterialTemplateEnum;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: MaterialTemplateServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:53:27
 */
@Implements(MaterialTemplateService.class)
public class MaterialTemplateServiceImpl implements MaterialTemplateService {

	@Override
	public Long createMaterialTemplate(MaterialTemplateEntity entity) {
		if (entity.getCheckedRequired() == null) {
			entity.setCheckedRequired(MaterialTemplateEnum.UNKNOWN);
		}
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getTemplateId();
	}

	@Override
	public void updateMaterialTemplate(MaterialTemplateEntity entity) {
		try {
			MaterialTemplateEntity persist = PersistUtil.get(MaterialTemplateEntity.class, entity.getTemplateId());
			BeanUtils.copyProperties(entity, persist);
			persist.setUpdateDateTime(DateUtil.now());
			PersistUtil.update(persist);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void disableMaterialTemplate(Long templateId) {
		MaterialTemplateEntity entity = PersistUtil.get(MaterialTemplateEntity.class, templateId);
		entity.setEnabled(Boolean.FALSE);
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	public List<MaterialTemplateEntity> inquireMaterialTemplateList(Long bulkstockCategoryId) {
		DetachedCriteria dc = DetachedCriteria.forClass(MaterialTemplateEntity.class);
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (bulkstockCategoryId != null) {
			dc.add(Restrictions.eq("bulkstockCategoryId", bulkstockCategoryId));
		}
		List<MaterialTemplateEntity> templateEntityPageList = PersistUtil.get(dc);
		return templateEntityPageList;
	}

	@Override
	public MaterialTemplateEntity inquireMaterialTemplateDetail(Long templateId) {
		MaterialTemplateEntity e = PersistUtil.get(MaterialTemplateEntity.class, templateId);
		if (e == null || !e.getEnabled()) {
			throw new BusinessException("商品融资材料模板不存在");
		}
		return e;
	}

}
